define(function(require, exports, module) {
	var _b = require('_b');
	var Product = require('../models/product');
	var Cart = require('../models/cart');
	window.cart = new Cart();
	
	var ProductList = _b.Collection.extend({		
		  url : HOST+'/front/product/service/id/'+category_id,
		  model: Product,
		  parse: function(response) {
			    return response.data;
		 },
	});
	
	
	var ProductItemView = _b.View.extend({
		  tagName: "li",
		  template:_.template($('#productItem').html()),
		  events: {
		    "click .btn-buy":  "add_cart",
		  },
		  add_cart:function(){
			  var cartItem = this.model.toJSON();
			  cartItem.buy_num = 1;
			  cart.add(cartItem);
			  _b.sync('create', this.model,{url:cart.url,success:function(data){
				  if(data.status == 0){
					  alert('加入购物车成功');
					  $('#cart_num').html(parseInt($('#cart_num').html())+1);
				  }else{
					  alert(data.msg);
				  }
			  }});
		  },
		  initialize: function() {
		  },
		  render: function() {
			  this.$el.html(this.template(this.model.toJSON()));
			  return this;
		  }
	});
	
	var ProductListView = _b.View.extend({
		 el: $('#productListView'),
		 initialize:function(){
		  },
		  initialize: function() {
			    this.listenTo(this.collection, "add", this.render);
			    this.listenTo(this.collection, "remove", this.render);
		},
		  renderItem:function($model,k){
			  var itemview = new ProductItemView({
		            model: $model
		        }); 
			  this.$el.append( itemview.render().el );
		  },
		  render: function() {
			  this.$el.html('');
			  this.collection.each(this.renderItem,this);
			  return this;
		  }
	});
	
	var list = new ProductList();
	var listView = new ProductListView({
		collection:list
	});
	list.fetch();
});